<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mvideo extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    public $mytable = "video";
    public $primaryKey = "VID_ID";
    public $resCode = "VIDEO";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData = array();
		$fieldData['SER_ID'] = 0;
		$fieldData['VID_TITLE'] = '';
		$fieldData['VID_CONTENT'] = '';
		$fieldData['VID_TIME'] = 0;
		$fieldData['VID_THUMB'] = '';
		$fieldData['VID_LINK'] = '';
		$fieldData['VID_TYPE'] = '';
		$fieldData['VID_CODE'] = '';
		$fieldData['VID_VIEW'] = 0;
		$fieldData['VID_TIMECREATE'] = 0;
		$fieldData['VID_TIMEUPDATE'] = 0;
		$fieldData['VID_STATUS'] = 1;
		$fieldData['VID_TIMESTART'] = 0;
		$fieldData['VID_TIMEEND'] = 0;
		$fieldData['SEO_H1'] = '';
		$fieldData['SEO_KEYWORD'] = '';
		$fieldData['SEO_DESCRIPTION'] = '';
		$fieldData['SEO_METATITLE'] = '';
		return $fieldData;
    }
	
	function updateView($videoID){
		//update view
        $this->db->where("VID_ID", $videoID);
        $this->db->set('VID_VIEW', 'VID_VIEW+1', false);
        $this->db->update($this->mytable, array());
	}
	
	function getsVideo($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
		
        return $this->db->get()->result();
    }
	
	// function gets_in ($array, $field ='', $limit = 10, $offset = 0) {
		// if ($field == '') {
			// $field = $this->primaryKey;
		// }
		
		// if ($limit > 0)
			// $this->db->limit($limit, $offset);
			
		// $this->db->from($this->mytable);
		// $this->db->where_in ($field, $array) ;
		// return $this->db->get()->result();
	// }
	
	function gets_by_series($seriesId, $limit = 10, $offset = 0) 
	{
		$this->db
			->order_by('VID_ID desc');
		if ($limit > 0)
			$this->db->limit($limit, $offset);
		$videos = $this->gets(array(
			'SER_ID' => $seriesId,
			'VID_STATUS' => 1
		));
		return $videos;
	}
	
	function gets_by_program($programId, $limit = 10, $offset = 0) {
		$this->load->model('mseries');
		$this->db
			->order_by('SER_ORDER desc')
			->order_by('SER_ID desc');
			
		$series = $this->mseries->get(array(
			'PROG_ID' =>$programId,
			'SER_STATUS' => 1));
		$videos = array();
		if (!empty($series)) {
			$this->db
				->order_by('VID_ID desc');
			if ($limit > 0)
				$this->db->limit($limit, $offset);
			$videos = $this->gets(array(
				'SER_ID' => $series->SER_ID,
				'VID_STATUS' => 1
			));
		}
		return $videos;
	}
}

?>